package cn.hxzy.company.controller;

import cn.hxzy.company.entity.User;
import cn.hxzy.company.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;

@Controller
public class LogController {
    @Autowired
    private UserService userService;
    /**
     * 用户登录
     */
    @GetMapping("/admin/login")
    public ModelAndView login() {
        ModelAndView modelAndView = new ModelAndView("admin/login");
        return modelAndView;
    }

    @PostMapping("/login")
    public String userLogin(User user, HttpSession session) {
        User user1 = userService.login(user);
        if (user1 != null) {
                session.setAttribute("loginUser", user);
                return "redirect:/admin/index";
        }
        session.setAttribute("info", "账户或密码有误");
        return "redirect:/admin/login";
    }
    /**
     * 用户登出
     */
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/admin/login";
    }
}
